package com.cxs.distributedencrypt;

import com.cxs.distributedencrypt.util.PrefUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Text;
import ohos.data.preferences.Preferences;
import ohos.rpc.IRemoteObject;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.RemoteException;

/**
 * 服务端
 */
public class ServiceAbility extends Ability {
    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "ServiceAbility");
    final static String KEY_CIPHER_TEXT = "KEY_CIPHER_TEXT";
    Preferences preferences;

    RemoteAgentStub remote = new RemoteAgentStub("") {
        @Override
        public String getCiphertext() throws RemoteException {
            if (preferences.hasKey(KEY_CIPHER_TEXT)) {
                String ciphertext = preferences.getString(KEY_CIPHER_TEXT, "");
                return ciphertext;
            }
            return null;
        }

        @Override
        public void setCiphertext(String _ciphertext) throws RemoteException {
            preferences.putString(KEY_CIPHER_TEXT, _ciphertext);
        }
    };

    @Override
    public void onStart(Intent intent) {
        HiLog.error(LABEL_LOG, "ServiceAbility::onStart");
        super.onStart(intent);
        preferences = PrefUtils.getPreferences(getContext());
    }

    @Override
    public IRemoteObject onConnect(Intent intent) {
        return remote;
    }

    @Override
    public void onDisconnect(Intent intent) {
    }
}